#include "BirConnection.h"
#include <BidirectionImpl.h>
#include <Ice/Ice.h>

BirConnection::BirConnection(const ::std::string& _clientId, Demo::CallbackSenderImpl* _pCallbackSenderImpl)
	:m_pCallbackSenderImpl(_pCallbackSenderImpl),
	m_ClientID(_clientId)
{
}


BirConnection::~BirConnection()
{
}

void BirConnection::SetClientCloseHandler(const std::shared_ptr<std::function<void()>>& _fun)
{
	m_pFun = _fun;
}

void BirConnection::heartbeat(const Ice::ConnectionPtr & connection)
{
	if (!connection)
		return;
	::Ice::ConnectionInfoPtr connInfo = connection->getInfo();
	::Ice::IPConnectionInfoPtr ipInfo = ::Ice::IPConnectionInfoPtr::dynamicCast(connInfo);
	//std::cout << "local Address::" << ipInfo->localAddress << std::endl;
	//std::cout << "remote Address::" << ipInfo->remoteAddress << std::endl;
}

void BirConnection::closed(const Ice::ConnectionPtr & connection)
{
	if (!connection)
		return;
	if (!m_pCallbackSenderImpl) {
		if (!m_pFun)
			return;
		(*m_pFun)();
		//客户端
		throw ::Ice::CloseConnectionException("连接关闭", 1);
	}
	m_pCallbackSenderImpl->removeClient(m_ClientID);
}
